// JavaScript Document
function Fileds(Edit){
	var EditMode=Edit;		//是否是显示多选框和编辑标记
	var filedTable=null;	//当前的表格
	var SelectedId=","			//所有选中的KeyId,用逗号分割
	var LastSelectedId=""			//最后一次选择的ID
	
	this.SetTitles=SetShow;
	this.Insert=InsterFiled;
	this.Select=doSelect;
	//返回值
	this.CheckedId=GetCheckId;
	this.EditId=GetEditId;
	//设置标题
	function SetShow(Titles){
		var tableCount=1;
		while(document.getElementById("FiledsTable_"+tableCount)){
			tableCount++;
		}
		document.write("<table id=\"FiledsTable_"+ tableCount +"\" class=\"Fileds\" width=\"100%\"></table>");
		filedTable=document.getElementById("FiledsTable_"+tableCount);
		//标题
		var filedTitle=filedTable.insertRow(0);

		var titlesTH=null;
		
		//选择列
		titlesTH=document.createElement("th");
		titlesTH.width="25";
		if(!EditMode){
			titlesTH.style.display="none";
		}
		filedTitle.appendChild(titlesTH);
		
		//编辑标记列
		 titlesTH=document.createElement("th");
		titlesTH.width="25";
		if(!EditMode){
			titlesTH.style.display="none";
		}
		titlesTH.innerHTML='<div class="filedEditSign">&nbsp;</div>';
		//titlesTH.className="filedEditSign";
		filedTitle.appendChild(titlesTH);

		for(var i=0;i<Titles.length;i++){
			titlesTH=document.createElement("th");
			titlesTH.innerHTML=Titles[i];
			filedTitle.appendChild(titlesTH);
		}
	}
	
	//插入记录 Count:位置 KeyId:值 Values:字段数组
	function InsterFiled(KeyId,Values,dbclick,Count){
		if(!Count){
			Count=-1;	
		}
		fileds=filedTable.insertRow(Count);
		
		fileds.id="filed_"+KeyId;
		fileds.onclick=function(){
			if(EditMode){
				document.getElementById("KeyId_"+KeyId).click();
			}
		}
		fileds.onmouseover=function(){
			if(!document.getElementById("KeyId_"+KeyId).checked){
				document.getElementById("filedEditSign_"+KeyId).className="moreEditSign";
			}
		}
		fileds.onmouseout=function(){
			if(!document.getElementById("KeyId_"+KeyId).checked){
				document.getElementById("filedEditSign_"+KeyId).className= "";
			}
		}
		if(dbclick){
				fileds.ondblclick=function(){
					eval(dbclick);
				}
		}
		var TDValue=null;
		

		//选择列
		TDValue=document.createElement("td");
		TDValue.innerHTML='<input type="checkbox" name="KeyId" id="KeyId_'+ KeyId +'" value="'+ KeyId +'" class="checkbox" />';
		if(!EditMode){
			TDValue.style.display="none";
		}
		fileds.appendChild(TDValue);
		//编辑标记列
		TDValue=document.createElement("td");
		TDValue.innerHTML='<div id="filedEditSign_'+ KeyId +'">&nbsp;</div>';
		if(!EditMode){
			TDValue.style.display="none";
		}
		//TDValue.id="filedEditSign_"+KeyId;
		fileds.appendChild(TDValue);
		
		for(var i=0;i<Values.length;i++){
			TDValue=document.createElement("td");
			TDValue.innerHTML=Values[i];
			fileds.appendChild(TDValue);
		}
		
		document.getElementById("KeyId_"+KeyId).onfocus=function(){this.blur();}
		//单击复选框时事件
		document.getElementById("KeyId_"+KeyId).onclick=function(){
			if(!EditMode){		//如果不是编辑模式，点击无效
				return false;
			}
			var checkbox=document.getElementById("KeyId_"+KeyId);
			if(checkbox.checked){
				SelectedId+=KeyId+",";

				document.getElementById("filed_"+KeyId).className="selected";
				if(LastSelectedId!=""){
					document.getElementById("filedEditSign_"+LastSelectedId).className="";
				}
				document.getElementById("filedEditSign_"+KeyId).className="filedEditSign";
				LastSelectedId=KeyId;
			}
			else{
				while(SelectedId.indexOf(","+ KeyId +",")>-1){
					SelectedId=SelectedId.replace(","+ KeyId +",",",");
				}
				document.getElementById("filed_"+KeyId).className="";
				document.getElementById("filedEditSign_"+KeyId).className="";
				GetEditId();
				if(LastSelectedId!=""){
					document.getElementById("filedEditSign_"+LastSelectedId).className="filedEditSign";
				}
			}
		}
		
	}
	
	//返回最后一次选中的值 
	function GetEditId(){
		var values=SelectedId.split(",");
		for(var i=values.length-1;i>0;i--){
			if(values[i]!=""){
				LastSelectedId=values[i];
				return LastSelectedId;
			}
		}
		return "";
	}
	
	//返回所有选中的值
	function GetCheckId(){
		if(!document.all.KeyId){
			return "";
		}
		var Checked="";
		var checkbox=document.all.KeyId;
		//alert("CK:"+checkbox.length);
		var i;
		for(i=0;i<checkbox.length;i++){
			if(checkbox[i].checked){
				if(Checked==""){
					Checked=checkbox[i].value;
				}
				else{
					Checked+=","+checkbox[i].value;
				}
			}
		}
		if(i==0){
			if(checkbox.checked){
				Checked=checkbox.value;
			}
		}
		
		return Checked;
	}
	//选择Way=1选择所有 2 取消选择所有 0反向选择
	function doSelect(Way){
		if(!document.all.KeyId){
			return;
		}
		var checkbox=document.all.KeyId;
		var i;
		for(i=0;i<checkbox.length;i++){
			switch(Way){
				case 0:
					checkbox[i].click();
					break;
				case 1:
					if(checkbox[i].checked==false){
						checkbox[i].click();
					}
					break;
				case 2:
					if(checkbox[i].checked==true){
						checkbox[i].click();
					}
					break;
			}
		}
		if(i==0){
			switch(Way){
				case 0:
					checkbox.click();
					break;
				case 1:
					if(checkbox.checked==false){
						checkbox.click();
					}
					break;
				case 2:
					if(checkbox.checked==true){
						checkbox.click();
					}
					break;
			}
		}
	}
}